package com.yanze.customviewdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import androidx.annotation.Nullable;

public class MyLocationMarkSV extends SurfaceView implements SurfaceHolder.Callback,Runnable {
    private static final String TAG = "yanze_surfaceView";
    private Paint mCirclePaint, mFillPaint, mSweepPaint;
    private float mRadius = 30;
    private float mSweepCurrRadius = mRadius + 2;
    private float mSweepRadius = mRadius + 30;

    private static final int COLOR_SWEEP = Color.parseColor("#6B0890C6");
    private static final int COLOR_NORMAL = Color.parseColor("#00A854");

    // SurfaceHolder
    private SurfaceHolder mHolder;
    // 用于绘图的Canvas
    private Canvas mCanvas;
    // 自线程标识位
    private boolean mIsDrawing;

    public MyLocationMarkSV(Context context) {
        this(context, null);
    }

    public MyLocationMarkSV(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLocationMarkSV(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);

        mFillPaint = new Paint();
        mFillPaint.setColor(COLOR_NORMAL);
        mFillPaint.setStyle(Paint.Style.FILL);

        mCirclePaint = new Paint();
        mCirclePaint.setColor(Color.WHITE);
        mCirclePaint.setStrokeWidth(3);
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setStyle(Paint.Style.STROKE);

        mSweepPaint = new Paint(mFillPaint);
        mSweepPaint.setColor(COLOR_SWEEP);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = (int) mSweepRadius * 2;
        int width = (int) mSweepRadius * 2;
        setMeasuredDimension(width, height);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Log.i(TAG, "surfaceCreated: ");
        mIsDrawing = true;
        new Thread(this).start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        Log.i(TAG, "surfaceChanged: ");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        Log.i(TAG, "surfaceDestroyed: ");
        mIsDrawing = false;
    }

    @Override
    public void run() {
        Log.i(TAG, "run: ");
        while (mIsDrawing) {
            draw();
        }
    }

    public void draw() {
        try {
            mCanvas = mHolder.lockCanvas();
            //...
            mCanvas.save();
            mCanvas.translate(getWidth() / 2, getHeight() / 2);
//            mSweepPaint.setColor(COLOR_SWEEP);
//            if (mSweepCurrRadius < mSweepRadius) {
//                mSweepCurrRadius += 2;
//                mCanvas.drawCircle(0, 0, mSweepCurrRadius, mSweepPaint);
//                postInvalidateDelayed(64);
//            }else{
//                mSweepCurrRadius = mRadius + 2;
//                postInvalidateDelayed(100);
//            }
            mCanvas.drawCircle(0, 0, mRadius - 3, mFillPaint);
            mCanvas.drawCircle(0, 0, mRadius - 3, mCirclePaint);
            mCanvas.drawCircle(0, 0, mSweepRadius, mSweepPaint);
            mCanvas.restore();
        } catch (Exception e) {
        }finally {
            if (mCanvas != null) {
                mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }
}
